import { Member } from '../models';

export default function (router) {
  /**
   * 后台设置会员接口
   */
  router.post('/api/admin/member', async ctx => {
    const { userid, memberName, days = 0 } = ctx.request.body;
    if (!userid || !memberName || (memberName === '自定义天数会员' && !days)) {
      await ctx.json(false, '参数错误');
      return;
    }
    const result = await Member.setMember({
      userid,
      memberName,
      days,
      source: 'admin-set'
    });
    if (result) {
      await ctx.json(true, '设置会员成功');
    } else {
      await ctx.json(true, '设置会员失败');
    }
  });

  /**
   * 后台删除会员
   */
  router.delete('/api/admin/member', async ctx => {
    const { userid } = ctx.request.body;
    if (!userid) {
      await ctx.json(false, '参数错误');
      return false;
    }
    const result = await Member.deleteMember(userid);
    if (result) {
      await ctx.json(true, '删除会员成功');
    } else {
      await ctx.json(true, '删除会员失败');
    }
  });
}
